package com.teacher.mobile.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.pojo.vo.role.*;
import com.teacher.service.IRoleService;
import com.teacher.pojo.entity.BaseEntity;
import com.teacher.pojo.entity.Role;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/pc/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;

    /**
     * 添加角色
     *
     * @param roleAddVo object of role's value
     * @return add role whether success
     */
    @PostMapping
    @ApiOperation("添加角色")
    @PreAuthorize("hasAuthority('user_role_add')")
    public CommonResult<String> addRole(@RequestBody RoleAddVo roleAddVo) {
        return iRoleService.addRole(roleAddVo);
    }

    /**
     * 更新角色
     *
     * @param roleUpdateVo object of role's value
     * @return update role whether success
     */
    @PutMapping
    @ApiOperation("更新角色")
    @PreAuthorize("hasAuthority('user_role_update')")
    public CommonResult<String> updateRole(@RequestBody RoleUpdateVo roleUpdateVo) {
        return iRoleService.updateRole(roleUpdateVo);
    }

    /**
     * 查询角色
     *
     * @param roleGetVo search role by page and name
     * @return page of role
     */
    @GetMapping
    @ApiOperation("查询角色")
    @PreAuthorize("hasAuthority('user_role_get')")
    public CommonResult<IPage<Role>> getRole(RoleGetDto roleGetVo) {
        return CommonResult.success(iRoleService.page(
                new Page<>(roleGetVo.getCurrentPage(), roleGetVo.getPageSize()),
                new QueryWrapper<Role>().lambda()
                        .like(roleGetVo.getName() != null, Role::getName, roleGetVo.getName())
                        .eq(roleGetVo.getStatus() != null, Role::getStatus, roleGetVo.getStatus())));
    }

    /**
     * @param ids about ids of role
     * @return delete role whether success
     */
    @DeleteMapping
    @ApiOperation("删除角色")
    @PreAuthorize("hasAuthority('user_role_delete')")
    public CommonResult<String> deleteRole(@RequestBody List<Long> ids) {
        if (iRoleService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }


    /**
     * @param name the keywords of role's name
     * @return limit 10 of roles
     */
    @GetMapping("/all")
    @ApiOperation("查询所有角色")
    public CommonResult<List<Role>> searchRole(String name) {
        return CommonResult.success(iRoleService.list(new QueryWrapper<Role>().lambda().orderByAsc(BaseEntity::getCreateTime)
                .eq(Role::getStatus, true)));
    }

    /**
     * update permission by role's id
     *
     * @return
     */
    @PutMapping("/permission")
    @ApiOperation("修改角色权限")
    @PreAuthorize("hasAuthority('user_role_menu_update')")
    public CommonResult<String> updatePermission(@RequestBody RoleUpdatePermissionVo roleUpdatePermissionVo) {
        return iRoleService.updatePermissionByRoleId(roleUpdatePermissionVo);
    }

    /**
     * 修改角色状态
     *
     * @param dto
     * @return
     */
    @PutMapping("/status")
    @ApiOperation("修改角色状态")
    @PreAuthorize("hasAuthority('user_role_menu_update')")
    public CommonResult<String> updateStatus(@RequestBody RoleUpdateStatusDto dto) {
        Role role = new Role();
        role.setId(dto.getId());
        role.setStatus(dto.getStatus());
        iRoleService.updateById(role);
        return CommonResult.success("修改成功");
    }
}

